package com.gjx.clock

import android.content.Intent
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SortedList
import com.gjx.clock.entity.NormalAlarmClock
import com.gjx.clock.service.ForwardService
import com.gjx.clock.ui.NormalClockSettingsActivity
import java.util.*
import kotlin.concurrent.thread

class NormalAdapter(
    private val viewModel: ClockViewModel,
    private val fragment: Fragment,
    private val normalAlarmClockList: SortedList<NormalAlarmClock>
) : RecyclerView.Adapter<NormalAdapter.ViewHolder>() {
    private var onBind: Boolean = true

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val normalClockTime = view.findViewById(R.id.normalClockTime) as TextView
        val enable = view.findViewById(R.id.enableNormal) as Switch
        val deleteNormalClock: ImageButton = view.findViewById(R.id.deleteNormalClock)
        val everyday = view.findViewById<TextView>(R.id.everyday)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(
            R.layout.normal_clock_item,
            parent, false
        )
        val holder = ViewHolder(view)

        view.setOnClickListener {
            val position = holder.adapterPosition
            val clock = normalAlarmClockList[position]
            val intent = Intent(fragment.context,NormalClockSettingsActivity::class.java)
            intent.putExtra("clockId",clock.id)
            fragment.startActivity(intent)
        }

        holder.enable.setOnCheckedChangeListener { _, isChecked ->
            val position = holder.adapterPosition
            val clock = normalAlarmClockList[position]


            // 如果和开关和闹钟的状态不同，也就是人为地动了开关，才执行下面
            if(isChecked!=clock.enable){
                if(isChecked){
                    AlarmHelper.setAlarm(fragment.context!!,clock)
                }else{
                    AlarmHelper.cancelAlarm(fragment.context!!,clock,true)
                }
                // 更新数据
                clock.enable = isChecked
                thread {
                    viewModel.updateClock(clock)
                }
            }
        }
        holder.deleteNormalClock.visibility=View.GONE
        holder.deleteNormalClock.setOnClickListener {
            val position = holder.adapterPosition
            val clock = normalAlarmClockList[position]

            AlarmHelper.cancelAlarm(fragment.context!!,clock)
            thread {
                viewModel.deleteClock(clock)
                fragment.activity?.runOnUiThread{
                    notifyItemRemoved(position)
//                        notifyDataSetChanged()
                }
            }
        }
        return holder
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        onBind = true
        val clock = normalAlarmClockList[position]
        holder.normalClockTime.text = clock.toString()
        holder.enable.isChecked = clock.enable
        holder.everyday.text = if(clock.everday) "每天" else "仅响铃一次"
        // 打开应该打开的闹钟
        if(clock.enable){
            AlarmHelper.setAlarm(fragment.context!!,clock,true)
        }

        onBind = false
    }

    override fun getItemCount() = normalAlarmClockList.size()




}

